package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.*;
import com.gljx.web.dao.CarmaintainMapper;
import com.gljx.web.dao.CarmaintainentryMapper;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.MessageMapper;
import com.gljx.web.service.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Administrator on 2017/11/20 0020.
 */
@Transactional
@Service
public class CarmainTainServiceImpl extends CommonDao implements CarmainTainService {
    @Autowired
    CarmaintainMapper carmaintainMapper;
    @Autowired
    CarmaintainentryMapper carmaintainentryMapper;
    //导入activiti工具
    ActitiviUtil activitiUtil = new ActitiviUtil();
    @Autowired
    MessageService messageService;
    @Autowired
    MaintainItemService maintainItemService;
    @Autowired
    private MessageMapper messageDao;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    HttpSession session;

    @Autowired
    RepairShopService repairShopService;


    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.CarmaintainMapper.pagecount", paramMap, null);
        return paramMap;
    }

    //提交流程
    private void submissionProcess(Carmaintain carmaintain) {

        //2.将这个单据编号和你需要保存的流程变量存入map集合中
        Map<String, Object> map = new HashMap<String, Object>();
        User user = (User)session.getAttribute("user");
        map.put(ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                ? ActivitiKey.CLWX_KEY_GL
                : ActivitiKey.CLWX_KEY_NC, carmaintain.getFnumber());//使用你自己单据的key，或者用默认的
        /*com.gljx.constant.ActivitiKey 中存入流程相关的常量，最好将流程定义的key也存入其中*/
        //行政部
        if ("CxzPUUR3QXqyZTabny04xsznrtQ=".equals(carmaintain.getCfdepartmentid()) || "fs/2yMXqT/2hQ62NDp9GosznrtQ=".equals(carmaintain.getCfdepartmentid())) {
            map.put(ActivitiKey.CAR, "1");
        }
        //教务部
        else if ("x17qPRcZRtiw8yENNOczWMznrtQ=".equals(carmaintain.getCfdepartmentid()) || "dxCXkvSKQKqyG/+/fNpdbsznrtQ=".equals(carmaintain.getCfdepartmentid())) {
            map.put(ActivitiKey.CAR, "2");
        }
        //考场部
        else if ("/62ZWcT+QzyllV/J2UgdosznrtQ=".equals(carmaintain.getCfdepartmentid()) || "fL7w8rSBQQmCy8e+xPuNJ8znrtQ=".equals(carmaintain.getCfdepartmentid())) {
            map.put(ActivitiKey.CAR, "3");
        }
        //营销部
        else if ("M/uf3IEoQYyYRZNhv+WrYMznrtQ=".equals(carmaintain.getCfdepartmentid()) || "YPQ5Pb6VSnqVWmHYVxqqTcznrtQ=".equals(carmaintain.getCfdepartmentid())) {
            map.put(ActivitiKey.CAR, "4");
        }

           float sum= Float.parseFloat(carmaintain.getSum());;
        if (sum > 200) {
            map.put(ActivitiKey.MONEY, "1");
        } else {
            map.put(ActivitiKey.MONEY, "0");
        }


        //3.调用方法
        String piID = activitiUtil.creatFlow(
                ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                        ? ActivitiKey.CLWX_KEY_GL
                        : ActivitiKey.CLWX_KEY_NC,  //指定要启动的流程定义
                map                     //流程变量
        );
        //4.如果你有“提交”这个流程，那么你还要补上一步，将流程推进到提交的下一步
        Message message = activitiUtil.completeFlowByPiID(piID);
        if (message != null) {
            message.setMessage("车辆维修-审核 #" + carmaintain.getFnumber() + "# ");//消息内容
            message.setUrl("/go_update_Carmaintain?fid=" + carmaintain.getFid());//详细路径
            message.setEditUrl("/queryCarmaintainExamine?fid=" + carmaintain.getFid()+"&fnumber=" + carmaintain.getFnumber());
            messageService.insert(message);
        }
    }

    //第二次提交
    private void submissionProcess2(Carmaintain carmaintain) {
        //2.将这个单据编号和你需要保存的流程变量存入map集合中
        Map<String, Object> map = new HashMap<String, Object>();
        User user = (User)session.getAttribute("user");
        map.put(ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                ? ActivitiKey.CLWX_KEY_GL
                : ActivitiKey.CLWX_KEY_NC, carmaintain.getFnumber());//使用你自己单据的key，或者用默认的
        /*com.gljx.constant.ActivitiKey 中存入流程相关的常量，最好将流程定义的key也存入其中*/
        //行政部
        if ("CxzPUUR3QXqyZTabny04xsznrtQ=".equals(carmaintain.getCfdepartmentid()) || "fs/2yMXqT/2hQ62NDp9GosznrtQ=".equals(carmaintain.getCfdepartmentid())) {
            map.put(ActivitiKey.CAR, "1");
        }
        //教务部
        else if ("x17qPRcZRtiw8yENNOczWMznrtQ=".equals(carmaintain.getCfdepartmentid()) || "dxCXkvSKQKqyG/+/fNpdbsznrtQ=".equals(carmaintain.getCfdepartmentid())) {
            map.put(ActivitiKey.CAR, "2");
        }
        //考场部
        else if ("/62ZWcT+QzyllV/J2UgdosznrtQ=".equals(carmaintain.getCfdepartmentid()) || "fL7w8rSBQQmCy8e+xPuNJ8znrtQ=".equals(carmaintain.getCfdepartmentid())) {
            map.put(ActivitiKey.CAR, "3");
        }
        //营销部
        else if ("M/uf3IEoQYyYRZNhv+WrYMznrtQ=".equals(carmaintain.getCfdepartmentid()) || "YPQ5Pb6VSnqVWmHYVxqqTcznrtQ=".equals(carmaintain.getCfdepartmentid())) {
            map.put(ActivitiKey.CAR, "4");
        }

        if (Float.parseFloat(carmaintain.getSum()) > 200) {
            map.put(ActivitiKey.MONEY, "1");
        } else {
            map.put(ActivitiKey.MONEY, "0");
        }
        Message message2 = (Message) activitiUtil.strictComplteFlowByVariable1(ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                ? ActivitiKey.CLWX_KEY_GL
                : ActivitiKey.CLWX_KEY_NC, carmaintain.getFnumber(), map
        );

        if (message2 != null) {
            message2.setMessage("车辆维修-审核 #" + carmaintain.getFnumber() + "# ");//消息内容
            message2.setUrl("/go_update_Carmaintain?fid=" + carmaintain.getFid());//详细路径
            message2.setEditUrl("/queryCarmaintainExamine?fid=" + carmaintain.getFid()+"&fnumber=" + carmaintain.getFnumber());

            messageService.insert(message2);
        }
    }

    //审核
    private String auditProcess(Carmaintain carmaintain, String agreed, String auditcomment) {

        if ("1".equals(agreed)) {
            auditcomment = "同意：" + auditcomment;
        } else {
            auditcomment = "不同意：" + auditcomment;
        }
        User user = (User)session.getAttribute("user");
        Object returnMsg =
                activitiUtil
                        .auditComplteMessage(
                                ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                                        ? ActivitiKey.CLWX_KEY_GL
                                        : ActivitiKey.CLWX_KEY_NC,
                                carmaintain.getFnumber(),                  //单据
                                Integer.valueOf(agreed),                 //1、0是否同意取决于你的流程图
                                auditcomment              //批注，直接把前端传过来的string塞进来
                        );
        if (returnMsg instanceof Message) {
            Message resultMsg = (Message) returnMsg;
            resultMsg.setMessage("车辆维修-审核 #" + carmaintain.getFnumber() + "# ");//消息内容
            resultMsg.setUrl("/go_update_Carmaintain?fid=" + carmaintain.getFid());//详细路径
            resultMsg.setEditUrl("/queryCarmaintainExamine?fid=" + carmaintain.getFid()+"&fnumber=" + carmaintain.getFnumber());
            Item item = new Item();
            item.msgSetItem(resultMsg);
            messageDao.insertSelective(item);
            return "审核成功！";
        } else if (activitiUtil.notAssign.equals(returnMsg)) {
            return "当前用户不是要求的经办人";

        } else if (activitiUtil.FINISH.equals(returnMsg)) {
            carmaintain.setCfbillstate(Long.valueOf("5"));

            //获取操作人信息
           // User user = (User) session.getAttribute("user");

            //获取当前修改时间
            String createTime = TimeFormat.timeFormat(new Date());
            //修改时间
            carmaintain.setFlastupdatetime(createTime);
            //修改人id
            carmaintain.setFauditorid(user.getFid());

            carmaintainMapper.updateByPrimaryKeySelective(carmaintain);
            return "此流程成功推进，并且此流程已经结束";

        } else if (activitiUtil.SUCCESS.equals(returnMsg)) {
            return "此流程成功推进";

        } else {
            return "没有找到正在进行的相关流程";
        }
    }

    /**
     * @Author lcx
     * @Date 2018/2/22 0022 下午 5:17
     * auditcomment 备注
     */
    //反审核
    private String againstAuditProcess(Carmaintain carmaintain, String auditcomment) {
        User user = (User)session.getAttribute("user");
        //如果是流程结束的反审核
        if ("endTure".equals(carmaintain.getAuditState())) {
            Message message2 = activitiUtil.getMessage(
                    ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                            ? ActivitiKey.CLWX_KEY_GL
                            : ActivitiKey.CLWX_KEY_NC,
                    carmaintain.getFnumber()                  //单据
            );

            if (message2 != null) {
                message2.setMessage("车辆维修-反审核 #" + carmaintain.getFnumber() + "# ");//消息内容
                message2.setUrl("/go_update_Carmaintain?fid=" + carmaintain.getFid());//详细路径
                messageService.insert(message2);
            }
        }
        //流程中的反审核
        else {
            Object result = activitiUtil.jumpActivtiMessage(
                    ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                            ? ActivitiKey.CLWX_KEY_GL
                            : ActivitiKey.CLWX_KEY_NC,
                    carmaintain.getFnumber(),                  //单据
                    auditcomment
            );
            if (result instanceof Message) {
                Message resultMsg = (Message) result;
                resultMsg.setMessage("车辆维修-反审核 #" + carmaintain.getFnumber() + "# ");//消息内容
                resultMsg.setUrl("/go_update_Carmaintain?fid=" + carmaintain.getFid());//详细路径
                messageService.insert(resultMsg);
            } else if (activitiUtil.SUCCESS.equals(result)) {
                System.out.println("成功");

            } else if (activitiUtil.notActiviti.equals(result)) {
                System.out.println("找不到流程节点");

            } else if (activitiUtil.notTask.equals(result)) {
                System.out.println("没有找到任务");
            }
        }

        carmaintain.setCfbillstate(Long.valueOf("3"));
        carmaintainMapper.updateByPrimaryKeySelective(carmaintain);
        return "成功！";

    }

    //判断当前用户可以审核该单据
    @Override
    public String selectAuditPerson(Object fnumber) {
        User user = (User)session.getAttribute("user");
        //2.查询
        String result = activitiUtil.getTaskStateByUser(
                ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                        ? ActivitiKey.CLWX_KEY_GL
                        : ActivitiKey.CLWX_KEY_NC,
                fnumber                  //单据
        );
        if (activitiUtil.notTask.equals(result)) {
            System.out.println("改单据没有对应流程");
            return "false";
        } else if (activitiUtil.FINISH.equals(result)) {
            System.out.println("流程已结束");
            return "endTure";
        }//剩下两种是该任务进行中
        else if (activitiUtil.notAssign.equals(result)) {
            System.out.println("经办人不匹配");
            return "false";
        } else if (activitiUtil.ISPOWER.equals(result)) {
            System.out.println("当前用户可以审核该单据");
            return "true";
        } else {
            return "false";
        }
    }

    //判断改单据没有对应流程
    @Override
    public boolean selectAuditPerson2(Object fnumber) {
        User user = (User)session.getAttribute("user");
        //2.查询
        String result = activitiUtil.getTaskStateByUser(
                ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                        ? ActivitiKey.CLWX_KEY_GL
                        : ActivitiKey.CLWX_KEY_NC,
                fnumber                  //单据
        );
        if (activitiUtil.notTask.equals(result)) {
            System.out.println("改单据没有对应流程");
            return false;
        } else if (activitiUtil.FINISH.equals(result)) {
            System.out.println("流程已结束");
            return true;
        }//剩下两种是该任务进行中
        else if (activitiUtil.notAssign.equals(result)) {
            System.out.println("经办人不匹配");
            return true;
        } else if (activitiUtil.ISPOWER.equals(result)) {
            System.out.println("当前用户可以审核该单据");
            return true;
        } else {
            return true;
        }
    }

    /**
     * @Author lcx
     * @Date 2018/3/1 0001 下午 2:26
     * 提交流程
     */
    @Override
    public void submit(Carmaintain carmaintain, String auditstate) {
        if ("true".equals(auditstate)) {
            submissionProcess2(carmaintain);
        } else {
            submissionProcess(carmaintain);
        }
        carmaintain.setCfbillstate(Long.valueOf("4"));
        carmaintainMapper.updateByPrimaryKeySelective(carmaintain);
    }

    @Override
    public List<Carmaintain> selectCarmaintainXls(String jlName, String cfcoachno, String beginTime, String endTime,
                                                  String fcompanyid,String departmentid, String cfmaintetype, String repairshopid) {
        return carmaintainMapper.selectCarmaintainAsXls(jlName, cfcoachno, beginTime, endTime, fcompanyid, departmentid, cfmaintetype, repairshopid);

    }
    @Override
    public List<Carmaintain> selectCarmaintainXls(String jlName, String cfcoachno, String beginTime, String endTime,
                                                  String fcompanyid) {
        return carmaintainMapper.selectCarmaintainAsXls(jlName, cfcoachno, beginTime, endTime, fcompanyid, "", "", "");

    }

    @Override
    public int importCarmainTainSG(HttpServletRequest request, List<Carmaintain> carmaintains, List<CarmaintainImport> carmaintainImports,User user,String[]carmainDateAll,String[]carmainCarnameAll) {
        //生成一个主表fid存储数组
        String[]fids=new String[carmaintains.size()];
        //车辆维修主表添加
        int j=0;
      List<Carmaintain> carmaintainList=new ArrayList<>();
        for(Carmaintain carmaintain:carmaintains){
            //*获取uuid
            String uuid = GetFid.UUIDIntoFid();
            //获取fid
            carmaintain.setFid(uuid);
            //获取维修项目
            carmaintain.setCfmaintetype(carmaintainImports.get(0).getCfmaintainitemtype());
            //部门id
           List<Department>departments= departmentService.selectAdminListAsfcontid(carmaintainImports.get(0).getCfdepartmentName(),user.getFcompanyid());
            carmaintain.setCfdepartmentid(departments.get(0).getFid());
            carmaintain.setCfdepartmentName((String) departments.get(0).getFnameL2());
            //存储字符串
            fids[j]=uuid;
            j++;
            carmaintain.setCfbillstate(3L);

            //添加入
            carmaintainList.add(carmaintain);
//添加订单号码
            String fnumber = GetFnumber.fnumbers("WXTEST")+j;

            carmaintain.setFnumber(GetFnumber.fnumbers("WXTEST")+j);
            //保存主表
        carmaintainMapper.insertSelective(carmaintain);
        }
//        这里对车辆维修子表进行提添加,而且不考虑对于是否存在旧表的情况
        String carName="";
// int i=0;
 //fseq
        int s=1;
        int type=0;

        String contrastFid="";
        List<Carmaintainentry> carmaintainentryList=new ArrayList<>();
        for (CarmaintainImport carmaintainImport:carmaintainImports) {
            Carmaintainentry carmaintainentry = new Carmaintainentry();
            carmaintainentry.setFid(GetFid.UUIDIntoFid());
//            if ("1".equals(state)) {
//                carmaintainentry.setFparentid(carmaintain.getFid());
//            } else {
//                carmaintainentry.setFparentid(uuid);
//            }
            //如果维修项目id为空 说明 是手动输入的维修项目 所以需要在维修项目表增加一条记录
//            if (StringUtils.isEmpty(cfmaintainitemid[i])){
//                MaintainItem maintainItem = new MaintainItem();
//                String uuid1  = GetFid.UUIDIntoFid();
//                //利用UUID生成fid
//                maintainItem.setFid(uuid1);
//                //公司id
//                maintainItem.setCfficompanyid(user.getFcompanyid());
//                //控制单元id
//                maintainItem.setFcontrolunitid(user.getFcompanyid());
//                //创建人id
//                maintainItem.setFcreatorid(user.getFid());
//                //获取当前时间
//                String createTime = TimeFormat.timeFormat(new Date());
//                //当前创建时间
//                maintainItem.setFcreatetime(createTime);
//                //修理厂id
//                maintainItem.setCfrepairshopid(cfrepairshopid[i]);
//                //维修项目名称
//                maintainItem.setFnameL2(cfmaintainitemName[i]);
//                //单价
//                maintainItem.setCfprice(new BigDecimal(cfprice[i] == null || cfprice[i] == "" ? "0" : cfprice[i]));
//                maintainItemService.addMaintainItem(maintainItem);
//            }
//            else {
//            }
              List<MaintainItem> maintainItems=maintainItemService.queryMaintainItemByName(carmaintainImport.getCfmaintainitemName(),user.getFcompanyid());
                if(maintainItems==null){
                    type=-2;
                    return type;
                }else {
                    carmaintainentry.setCfmaintainitemid(maintainItems.get(0).getFid());
                    List<RepairShop> repairShopList = repairShopService.queryRepairShopAllAsName((carmaintainImport.getCfrepairshopName().replace(" +","")));
                    carmaintainentry.setCfrepairshopid(repairShopList.get(0).getFid());
                    carmaintainentry.setCfmanhours(carmaintainImport.getCfmanhours());//工时
                    carmaintainentry.setCfbasemanhours(carmaintainImport.getCfpriceofhours());//工时金额
                    carmaintainentry.setCfamount(carmaintainImport.getCfpriceofCarmain());//维修金额
                    carmaintainentry.setCfgsqty(carmaintainImport.getCfcountofhours());//工时数量
                    carmaintainentry.setCfprice(carmaintainImport.getCfprice());//单价
                    carmaintainentry.setCfqty(carmaintainImport.getCfamount());//数量
                    carmaintainentry.setCfallamount(carmaintainImport.getCfallamount());//合计
                    carmaintainentry.setCfcarName(carmaintainImport.getCfcarName());//车辆名称
                    carmaintainentry.setCfremark(" ");

                    for(int h=0;h<carmaintainList.size();h++){

                        if(carmaintainentry.getCfcarName().equals(carmaintainList.get(h).getCfcarName())){
                            carmaintainentry.setFparentid(carmaintainList.get(h).getFid());

                        }
                    }
                    carmaintainentryList.add(carmaintainentry);


//                    carmaintainentryMapper.insertSelective(carmaintainentry);
                }
        }
        String name="";
        Long num=1L;
        for(int z=0;z<carmaintainentryList.size();z++){
            carmaintainentryList.get(z).setFseq(num);
               if(!"".equals(name)&&name.equals(carmaintainentryList.get(z).getFparentid())){
                   num++;
               }
               name=carmaintainentryList.get(z).getFparentid();

            carmaintainentryMapper.insertSelective(carmaintainentryList.get(z));
            type=1;
        }

         if(carmaintainentryList==null){
            type=-2;
            return type;
         }else{

        return type;
         }


    }

    @Override
    public void addCarmaintain(String state, Carmaintain carmaintain, String[] cfmaintainitemid,
                               String[] cfmaintainitemName, String[] cfrepairshopid, String[] cfmanhours,
                               String[] cfbasemanhours, String[] cfgsqty, String[] cfprice, String[] cfqty,
                               String[] cfamount, String[] cfallamount, String[] cfremark, String auditstate) {

        User user = (User)session.getAttribute("user");

        //删除老的信息在次添加
        if ("1".equals(state)) {
            //查询之前的信息
            Carmaintain carmaintaintem = carmaintainMapper.selectByPrimaryKey(carmaintain.getFid());
            carmaintain.setFcreatorid(carmaintaintem.getFcreatorid());
            carmaintain.setFcreatetime(carmaintaintem.getFcreatetime());
            //carmaintainMapper.deleteByPrimaryKey(carmaintain.getFid());
            carmaintainentryMapper.deleteByFparentid(carmaintain.getFid());
            //为了消除提交页面点击确定丢失审核人的 问题
            if ("2".equals(carmaintain.getFfivouchered() + "")) {
                carmaintain.setFauditorid(carmaintaintem.getFauditorid());
                carmaintain.setCfbillstate(Long.valueOf("3"));
            }
            if ("4".equals(carmaintain.getFfivouchered() + "")) {
                //判断是不是第一次提交流程 如果是第一次提交则创建流程
                if ("true".equals(auditstate)) {
                    submissionProcess2(carmaintain);
                } else {
                    submissionProcess(carmaintain);
                }
            }
        }

        //*获取uuid
        String uuid = GetFid.UUIDIntoFid();
        if (cfmaintainitemid != null || cfmaintainitemid.length != 0) {
            //添加子类

            for (int i = 0; i < cfmaintainitemid.length; i++) {
                Carmaintainentry carmaintainentry = new Carmaintainentry();

                carmaintainentry.setFid(GetFid.UUIDIntoFid());
                if ("1".equals(state)) {
                    carmaintainentry.setFparentid(carmaintain.getFid());
                } else {
                    carmaintainentry.setFparentid(uuid);
                }
                //如果维修项目id为空 说明 是手动输入的维修项目 所以需要在维修项目表增加一条记录
                if (StringUtils.isEmpty(cfmaintainitemid[i])){
                    MaintainItem maintainItem = new MaintainItem();
                    String uuid1  = GetFid.UUIDIntoFid();
                    //利用UUID生成fid
                    maintainItem.setFid(uuid1);
                    //公司id
                    maintainItem.setCfficompanyid(user.getFcompanyid());
                    //控制单元id
                    maintainItem.setFcontrolunitid(user.getFcompanyid());
                    //创建人id
                    maintainItem.setFcreatorid(user.getFid());
                    //获取当前时间
                    String createTime = TimeFormat.timeFormat(new Date());
                    //当前创建时间
                    maintainItem.setFcreatetime(createTime);
                    //修理厂id
                    maintainItem.setCfrepairshopid(cfrepairshopid[i]);
                    //维修项目名称
                    maintainItem.setFnameL2(cfmaintainitemName[i]);
                    //单价
                    maintainItem.setCfprice(new BigDecimal(cfprice[i] == null || cfprice[i] == "" ? "0" : cfprice[i]));
                    maintainItemService.addMaintainItem(maintainItem);
                    carmaintainentry.setCfmaintainitemid(uuid1);
                }
                else {
                    carmaintainentry.setCfmaintainitemid(cfmaintainitemid[i]);
                }
                carmaintainentry.setCfrepairshopid(cfrepairshopid[i]);

                carmaintainentry.setCfmanhours(new BigDecimal(cfmanhours[i] == null || cfmanhours[i] == "" ? "0" : cfmanhours[i]));
                carmaintainentry.setCfbasemanhours(new BigDecimal(cfbasemanhours[i] == null || cfbasemanhours[i] == "" ? "0" : cfbasemanhours[i]));
                carmaintainentry.setCfgsqty(new BigDecimal(cfgsqty[i] == null || cfgsqty[i] == "" ? "0" : cfgsqty[i]));
                carmaintainentry.setCfprice(new BigDecimal(cfprice[i] == null || cfprice[i] == "" ? "0" : cfprice[i]));
                carmaintainentry.setCfqty(new BigDecimal(cfqty[i] == null || cfqty[i] == "" ? "0" : cfqty[i]));
                carmaintainentry.setCfamount(new BigDecimal(cfamount[i] == null || cfamount[i] == "" ? "0" : cfamount[i]));
                carmaintainentry.setCfallamount(new BigDecimal(cfallamount[i] == null || cfallamount[i] == "" ? "0" : cfallamount[i]));
                carmaintainentry.setCfremark(cfremark[i]);

                carmaintainentryMapper.insertSelective(carmaintainentry);
            }
            if ("1".equals(state)) {
                carmaintainMapper.updateByPrimaryKeySelective(carmaintain);
            } else {
                carmaintain.setFid(uuid);
                carmaintainMapper.insertSelective(carmaintain);

            }
        }
    }

    /**
     * @Author lcx
     * @Date 2017/12/16 0016 上午 9:40
     * 更新操作的查询
     */
    @Override
    public Carmaintain selectCarmainTainByFid(String fid) {
        Carmaintain carmaintain = carmaintainMapper.selectByPrimaryKey(fid);
        if (carmaintain != null) {
            carmaintain.setCarmaintainentryList(carmaintainentryMapper.selectCarmainTainentryByFparentid(fid));
        }
        return carmaintain;
    }

    @Override
    public void deleteByCarmainTainKey(String[] deleteId, String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();
        User user = (User)session.getAttribute("user");
        for (int i = 0; i < deleteId.length; i++) {
            actitiviUtil.deleteTask(ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid())
                    ? ActivitiKey.CLWX_KEY_GL
                    : ActivitiKey.CLWX_KEY_NC, fnumber[i]);
            carmaintainMapper.deleteByPrimaryKey(deleteId[i]);
            carmaintainentryMapper.deleteByFparentid(deleteId[i]);
        }
    }

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.CarmaintainMapper.pagemap", paramMap, null);
        return paramMap;
    }

    @Override
    public List<Carmaintainentry> selectCarMainTain(Map<String, String> map3) {
        // TODO Auto-generated method stub
        return carmaintainentryMapper.selectCarMainTain(map3);
    }

    @Override
    public Carmaintainentry selectCarMainTainCount(Map<String, String> map3) {
        // TODO Auto-generated method stub
        return carmaintainentryMapper.selectCarMainTainCount(map3);
    }

    @Override
    public String examine(Carmaintain carmaintain, String state, String agreed, String auditcomment) {
        //如果是审核
        if ("5".equalsIgnoreCase(state)) {
            String returnString = auditProcess(carmaintain, agreed, auditcomment);
            //如果是不同意状态则修改单据状态为保存
            if ("2".equals(agreed)) {
                carmaintain.setCfbillstate(Long.valueOf("3"));
                carmaintainMapper.updateByPrimaryKeySelective(carmaintain);
            }
            return returnString;
        }
        if ("3".equalsIgnoreCase(state)) {
            carmaintain.setFsourcefunction(auditcomment);
            return againstAuditProcess(carmaintain, auditcomment);
        }

        return "参数错误！";
    }
}
